import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.Pageobject.user_pages import UserPage
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.PageLocate.register_locator import RegisterLocator
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.Common.do_mysql import DoMysql


class  RegisterPages(Basepage):


#获取invite id
    def get_invite_id(self,email,cmdopt,database_object):
        database = Commondata.db_config[cmdopt]['database'][0]
        get_id_sql=Commondata.query_invite_id.replace('@a',email)
        data_object, pool = CommonTool(database_object).get_data_source(database, cmdopt)
        query_result = data_object.do_mysql(get_id_sql, pool)[0]['id']
        print(query_result)
        return query_result

#获取注册地址
    def get_register_url(self,invite_id,cmdopt):
        register_url=Commondata.register_url[cmdopt]['user'].replace('@a',invite_id)
        print(register_url)
        return register_url

#获取admin端的注册地址
    def get_admin_register_url(self,invite_id,cmdopt):
        register_url=Commondata.register_url[cmdopt]['admin'].replace('@a',invite_id)
        print(register_url)
        return register_url

#注册页面
    def user_rigister(self,register_url,email,cmdopt,get_database_source):
        doc='user端的用户注册'
        self.driver.get(register_url)
        time.sleep(5)
        self.wait_elevisble(RegisterLocator.send_button,doc)
        #获取注册验证码
        time.sleep(2)
        self.click_element(RegisterLocator.send_button,doc)
        time.sleep(2)
        #self.wait_elevisble(RegisterLocator.send_loading,doc)
        time.sleep(8)
        email_verification=BusinessCommonTools().get_verification_code(cmdopt,email,get_database_source)
        self.send_keys(RegisterLocator.email_verification,email_verification,doc)
        time.sleep(5)
        self.send_keys(RegisterLocator.new_pwd,Logindata.new_pwd,doc)
        time.sleep(2)
        self.send_keys(RegisterLocator.confirm_pwd,Logindata.confirm_pwd,doc)
        time.sleep(2)
        self.click_element(RegisterLocator.next_step_btn,doc)
        self.wait_elevisble(RegisterLocator.first_name_input,doc)
        first_name=CommonTool.generate_first_name()
        self.send_keys(RegisterLocator.first_name_input,first_name,doc)
        last_name=CommonTool.generate_last_name()
        self.send_keys(RegisterLocator.last_name_input,last_name,doc)
        # 20240401 增加country选择
        self.wait_elevisble(RegisterLocator.country,doc)
        title=self.get_attribute(RegisterLocator.country_text,'title',doc)
        print('title:',title)
        first_title=title[0]
        self.move_mouse_to_click_element(RegisterLocator.country,doc)
        time.sleep(2)
        if ord(first_title) < ord('C'):
            self.scroll_bar_operation2(RegisterLocator.country_china)
        if ord(first_title) > ord('C'):
            self.scroll_bar_operation2(RegisterLocator.country_china,down=2)
        time.sleep(2)
        self.wait_elevisble(RegisterLocator.country_china,doc)
        self.move_mouse_to_click_element(RegisterLocator.country_china,doc)
        phone_number=CommonTool.generate_phone_number()
        self.send_keys(RegisterLocator.phone_number,phone_number,doc)
        try:
            self.send_keys(RegisterLocator.sms_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        except:
            pass
        text=CommonTool.generate_string()
        time.sleep(20)
        self.send_keys(RegisterLocator.post_code,phone_number,doc)
        self.send_keys(RegisterLocator.state,text,doc)
        self.send_keys(RegisterLocator.city,text,doc)
        self.send_keys(RegisterLocator.address,text,doc)
        time.sleep(2)
        self.click_element(RegisterLocator.agree,doc)
        time.sleep(2)
        self.click_element(RegisterLocator.confirm_register,doc)
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_success_ele,doc)
        time.sleep(8)


    def register(self,cmdopt,get_database_source,acct_type):
        doc='注册页面'
        register_email=CommonTool.generate_email()
        self.driver.get(Commondata.user_register_ur[cmdopt])
        time.sleep(2)
        self.wait_elevisble(RegisterLocator.email_input,doc)
        self.send_keys(RegisterLocator.email_input,register_email,doc)
        self.click_element(RegisterLocator.send_button, doc)
        time.sleep(2)
        # self.wait_elevisble(RegisterLocator.send_loading,doc)
        time.sleep(8)
        email_verification = BusinessCommonTools().get_verification_code(cmdopt, register_email, get_database_source)
        self.send_keys(RegisterLocator.email_verification, email_verification, doc)
        time.sleep(5)
        self.send_keys(RegisterLocator.new_pwd, Logindata.new_pwd, doc)
        time.sleep(2)
        self.send_keys(RegisterLocator.confirm_pwd, Logindata.confirm_pwd, doc)
        time.sleep(2)
        self.click_element(RegisterLocator.next_step_btn, doc)
        self.wait_elevisble(RegisterLocator.first_name_input, doc)
        first_name = CommonTool.generate_first_name()
        self.send_keys(RegisterLocator.first_name_input, first_name, doc)
        last_name = CommonTool.generate_last_name()
        self.send_keys(RegisterLocator.last_name_input, last_name, doc)
        phone_number = CommonTool.generate_phone_number()
        self.send_keys(RegisterLocator.phone_number, phone_number, doc)
        try:
            self.send_keys(RegisterLocator.sms_verification_code, Logindata.verification_code[cmdopt]['code'], doc)
        except:
            pass
        text = CommonTool.generate_string()
        time.sleep(20)
        self.send_keys(RegisterLocator.post_code, phone_number, doc)
        self.send_keys(RegisterLocator.state, text, doc)
        self.send_keys(RegisterLocator.city, text, doc)
        self.send_keys(RegisterLocator.address, text, doc)
        time.sleep(2)
        self.click_element(RegisterLocator.agree, doc)
        self.wait_elevisble(RegisterLocator.confirm_register, doc)
        time.sleep(2)
        self.click_element(RegisterLocator.confirm_register, doc)
        self.wait_elevisble(RegisterLocator.register_success, doc)
        time.sleep(8)
        self.get_account_to_click(acct_type)
        time.sleep(2)
        self.click_element(RegisterLocator.active_button,doc)
        company_name=CommonTool.generate_string()
        self.wait_elevisble(RegisterLocator.company_name_input,doc)
        self.send_keys(RegisterLocator.company_name_input,company_name,doc)
        time.sleep(2)
        vat_number=CommonTool.generate_phone_number()
        self.send_keys(RegisterLocator.vat_number,vat_number,doc)
        company_phone_number=CommonTool.generate_phone_number()
        self.send_keys(RegisterLocator.phone_number,company_phone_number,doc)
        text = CommonTool.generate_string()
        self.send_keys(RegisterLocator.post_code, phone_number, doc)
        self.send_keys(RegisterLocator.state, text, doc)
        self.send_keys(RegisterLocator.city, text, doc)
        self.send_keys(RegisterLocator.address, text, doc)
        time.sleep(2)
        self.click_element(RegisterLocator.confirm_register, doc)
        self.wait_elevisble(RegisterLocator.company_register_success,doc)
        time.sleep(8)
        return register_email



    def  get_account_to_click(self,account_type):
        doc='获取账号类型然后点击'
        eles=self.get_elements(RegisterLocator.account_type,doc)
        for ele in eles:
            if ele.text==account_type:
                ele.click()


    def approval_register_query(self,account_email):
        doc='查询注册的账号'
        for i in range(3):
            try:
                self.driver.refresh()
                self.enter_approval_page()
                self.wait_elevisble(RegisterLocator.approval_search_input,doc)
                break
            except:
                self.driver.refresh()
                continue
        self.keyboard_event(RegisterLocator.approval_search_input,doc)
        self.send_keys(RegisterLocator.approval_search_input,account_email,doc)
        self.click_element(RegisterLocator.approval_search_input_button,doc)
        self.wait_elevisble(RegisterLocator.registration_data,doc)
        registration_list=self.get_elements(RegisterLocator.registration_data,doc)
        return  registration_list



    def enter_approval_page(self):
         time.sleep(5)
         doc = '进入到approval页面'
         time.sleep(5)
         for i in range(3):
            try:
                self.wait_elevisble(RegisterLocator.registration_tab, doc)
                time.sleep(2)
                self.click_element(RegisterLocator.registration_tab, doc)
                break
            except:
                 self.refresh_page()
                 time.sleep(5)
                 continue
         self.wait_elevisble(OrganizationLocator.page_total,doc)


    def reject_user(self,accout_email):
        doc='拒绝注册'
        for i in range(3):
            try:
                self.driver.refresh()
                self.approval_register_query(accout_email)
                self.wait_elevisble(RegisterLocator.approve_button,doc)
                self.move_mouse_to_click_element(RegisterLocator.approve_button, doc)
                time.sleep(2)
                self.wait_elevisble(RegisterLocator.reject_button, doc)
                break
            except:
                self.refresh_page()
                time.sleep(5)
                continue
        self.click_element(RegisterLocator.reject_button, doc)
        time.sleep(2)
        self.click_element(RegisterLocator.submit_button, doc)
        self.wait_elevisble(RegisterLocator.public_prompt, doc)




    def page_filter_function_search(self,accout_email):
        doc='筛选查询'
        self.enter_approval_page()
        self.approval_register_query(accout_email)
        self.wait_elevisble(RegisterLocator.account_type_filter,doc)
        self.click_element(RegisterLocator.account_type_filter,doc)
        try:
            self.wait_elevisble(RegisterLocator.enterprise_option,doc)
            self.click_element(RegisterLocator.enterprise_option,doc)
        except:
            pass
        time.sleep(2)
        self.click_element(RegisterLocator.filter_ok_button,doc)
        self.wait_elevisble(RegisterLocator.registration_data, doc)
        registration_list = self.get_elements(RegisterLocator.registration_data, doc)
        return registration_list


















